package com.brillilab.starter.controller.crm;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.crm.CrmUsersInfoVo;
import com.brillilab.domain.vo.user.InviteCodeSearchVo;
import com.brillilab.service.core.user.IInviteCodeService;
import com.brillilab.service.logic.crm.CrmUsersLogic;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author zlb
 * @Title: InviteManageController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/10/9 12:27
 */
@RestController
@RequestMapping("/crm/inviteManage")
public class InviteManageController {

    @Resource
    private CrmUsersLogic crmUsersLogic;
    @Resource
    private IInviteCodeService inviteCodeService;

    /**
     * 生成邀请码
     *
     * @param makeWay   使用途径
     * @param codeCount 邀请码数量
     * @param token     用户token
     * @return
     */
    @RequestMapping(value = "/generateCode", method = RequestMethod.GET)
    public ResponseVo generateInviteCode(String makeWay, Integer codeCount, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.isTrue(StringUtils.isNotBlank(makeWay), "邀请码使用途径不能为空！");
        Assert.notNull(codeCount, "邀请码数量不能为空！");
        Assert.isTrue(codeCount != 0, "邀请码数量有误！");
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        List<String> codes = inviteCodeService.generateInviteCode(makeWay, codeCount, crmUsersInfoVo.getRealName());
        HashMap<String, Object> rsMap = new HashMap<>();
        rsMap.put("codes", codes);
        return ResponseVo.success(rsMap);
    }


    /**
     * 实验室申请列表
     *
     * @param token
     * @param labApplyListVo
     * @return
     */
    @PostMapping("/inviteCodeList")
    public ResponseVo inviteCodeList(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestBody InviteCodeSearchVo inviteCodeSearchVo) {
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        Integer pageNum = inviteCodeSearchVo.getPageNum() == null || inviteCodeSearchVo.getPageNum() == 0 ? 1 : inviteCodeSearchVo.getPageNum();
        Integer pageSize = inviteCodeSearchVo.getPageSize() == null || inviteCodeSearchVo.getPageSize() == 0 ? 20 : inviteCodeSearchVo.getPageSize();
        Integer isDesc = inviteCodeSearchVo.getIsDesc() == null ? 1 : inviteCodeSearchVo.getIsDesc();
        return ResponseVo.success(inviteCodeService.selectByCondition(inviteCodeSearchVo.getUseType(), inviteCodeSearchVo.getMakeName(), isDesc, pageNum, pageSize));
    }

    /**
     * 使用途径列表
     * @return
     */
    @GetMapping("/getUseTypeList")
    public ResponseVo getUseTypeList(@RequestHeader(name = StarterConstant.TOKEN) String token) {
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        List<String> useTypeList = inviteCodeService.getUseTypeList();
        return ResponseVo.success(useTypeList);
    }
    /**
     * 生成人列表
     * @return
     */
    @GetMapping("/getMakeNameList")
    public ResponseVo getMakeNameList(@RequestHeader(name = StarterConstant.TOKEN) String token) {
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        List<String> makeNameList = inviteCodeService.getMakeNameList();
        return ResponseVo.success(makeNameList);
    }
}
